# -*- coding: utf-8 -*-
# @Time    : 2023/8/20
# @Author  : Yangyali
#
import loguru
from playwright.sync_api import sync_playwright, APIRequestContext,Response
from common.util import read_yaml, get_config_value

from common.login_utils import login


from playwright.sync_api import Playwright
class HttpReq():
    def __init__(self,playwright):
        self.request_context = playwright.request.new_context(
        base_url=get_config_value("production", "base_url")
    )
        self.token = login(self.request_context)
        self.headers = {
        "token": read_yaml("token"),
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
        }

    def get(self,url,params=None,headers=None)-> Response:
        response =  self.request_context.get(url=url, params=params,headers=self.headers)
        return response
    def post(self,url,data,headers=None)-> Response:
        response = self.request_context.post(url=url, data=data, headers=self.headers)
        return response
    def assert_response(self,response)-> Response:
        assert  response.ok
        assert  response.status == 200
        assert response.json()["msg"] == "操作成功"
        assert response.json()["code"]==0
# with sync_playwright() as playwright:
#     request=HttpReq(playwright)
#     response=request.get("/workspace/testplan")
#     request.assert_response(response)
#     loguru.logger.info("运行成功")
#     print(response.json())